/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-12-28 17:09:13
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */
console.log(Date.now("2021-11-15 10:00:23"));
const Service = require("egg").Service;
const timeFormart = require("../extends/timeFormart");
class HousingRingService extends Service {
    //获取列表
    async getHousingRingList() {
            const { ctx, app } = this;
            let res = await app.mysql.select("housingring");
            let imgRes = await app.mysql.select("imgs");
            let brokerRes = await app.mysql.select("broker");
            let result = [];
            res.forEach((item) => {
                const brokerInfo = brokerRes.find((val) => val.id === item.user_id);
                const filterList = item.imgSrc.split(",");
                const imgList = imgRes.filter((v) => {
                    const index = filterList.findIndex((value) => value == v.key);
                    if (index !== -1) {
                        return v;
                    }
                });
                const imgSrc = imgList.map((item) => {
                    return item.url;
                });
                result.push({
                    imgSrc: imgSrc,
                    name: brokerInfo.name,
                    avatar: brokerInfo.avatar,
                    time: item.time,
                    title: item.title,
                    id: item.id,
                });
            });
            result.sort((a, b) => {
                return new Date(b.time) - new Date(a.time);
            });
            return result;
        }
        //删除
    async delHousingRingList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("housingring", { id });
            return res;
        }
        //发布
    async publicHousingRingList() {
        const { ctx, app } = this;
        const { imgSrc, user_id, title, telephone } = ctx.request.body;
        let arr = [];
        imgSrc.forEach((item, index) => {
            const date = Date.now() + index;
            app.mysql.insert("imgs", {
                url: item,
                create_time: timeFormart(Date.now()),
                key: date,
                telephone,
            });
            arr.push(date);
        });
        const res = await app.mysql.insert("housingring", {
            user_id: user_id,
            title: title,
            imgSrc: String(arr),
            time: timeFormart(Date.now()),
        });
        return res;
    }
}
module.exports = HousingRingService;